package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.InventoryInoutJob;
import com.wing.order.model.entity.InventoryJobSignProductDetail;
import com.wing.order.model.response.InventoryInoutJobVO;
import com.wing.order.model.response.InventoryInoutVO;
import com.wing.order.model.response.InventoryJobLabelVO;
import com.wing.order.model.response.OrderDeliverySignVO;
import com.wing.order.service.*;
import com.wing.warehouse.model.response.PalletBoxListVO;
import com.wing.warehouse.service.PalletBoxListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@Api(value = "Admin-入库作业管理", tags = "#/order/storageWork")
@RestController("storageWorkController")
@RequestMapping("order/admin/storageWork")
@RequiredArgsConstructor
public class StorageWorkController {
    private final InventoryInoutJobService inventoryInoutJobService;
    private final InventoryInoutService inventoryInoutService;
    private final PalletBoxListService palletBoxListService;
    private final InventoryJobLabelService inventoryJobLabelService;
    private final OrderDeliverySignService orderDeliverySignService;
    private final InventoryJobSignProductDetailService inventoryJobSignProductDetailService;

    @ApiOperation(value = "查询所有入库作业列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryInoutJobVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryInoutJob inventoryInoutJob
    ){


        //搜索
        IPage<InventoryInoutJobVO> list = inventoryInoutJobService.getInventoryInoutJobPageList(inventoryInoutJob,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "查询计划详情信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "入库单id", required = true, dataType = "Long", paramType = "query")
    })
    @GetMapping("/getInventoryInoutVOInfo")
    public JsonResult<InventoryInoutVO> getInventoryInoutVOInfo(Long planId) {
        InventoryInoutVO info = inventoryInoutService.getInventoryInoutVOInfo(planId);
        return JsonResult.success(info);
    }

    @ApiOperation(value = "查询料箱数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "料箱编号", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/getPalletBox")
    public JsonResult<PalletBoxListVO> getPalletBox(String code,Long warehouseId){
        PalletBoxListVO palletBox = palletBoxListService.getPalletBox(code,warehouseId);
        if(palletBox == null){
            return JsonResult.fail("料箱不存在");
        }
        return JsonResult.success(palletBox);
    }

    @ApiOperation(value = "查询入库计划产品的料箱", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuCode", value = "skuCode编号", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "warehouseId", value = "仓库id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getPLanProductBoxByJobProductId")
    public JsonResult<List<PalletBoxListVO>> getPLanProductBoxByJobProductId(String skuCode, Long warehouseId){
        List<PalletBoxListVO> boxListVOList = palletBoxListService.getPLanProductBoxByJobProductId(skuCode, warehouseId);
        /*if(CollectionUtils.isEmpty(boxListVOList)){
            return JsonResult.fail("此产品还未录入料箱");
        }*/
        return JsonResult.success(boxListVOList);
    }
    @ApiOperation(value = "入库作业/扫描包装条码", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signId", value = "签收id", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "code", value = "包装条码", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "inoutType", value = "出入库类型:in(1,入库);out(2,出库)", required = false, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getSignPackLabel")
    public JsonResult<InventoryJobLabelVO> getSignPackLabel(Long signId,String code,Integer inoutType,Long signProductDetailId){
        InventoryJobLabelVO jobLabelVO = inventoryJobLabelService.getSignPackLabel(signId, code, inoutType,signProductDetailId);
        if(jobLabelVO == null){
            return JsonResult.fail("未找到包装条码");
        }
        if(jobLabelVO.getStatus().name().equals("storage")){
            return JsonResult.fail("此包装条码已入库，请勿重复扫码");
        }
        return JsonResult.success(jobLabelVO);
    }

    @ApiOperation(value = "添加入库作业单记录", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boxCode", value = "料箱编码", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "signStationName", value = "入库作业人", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "remark", value = "入库作业说明", required = false, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "planId", value = "计划id", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "jobLabelList", value = "入库条码数据", required = true, dataType = "List", paramType = "path")
    })
    @PostMapping("/addStorageWork")
    public JsonResult addStorageWork(@RequestBody InventoryInoutJobVO inventoryInoutJobVO) {
        try {
            inventoryInoutJobService.addStorageWork(inventoryInoutJobVO);
            return JsonResult.success("操作成功！");
        }catch (Exception e){
            log.error("addStorageWork:", e);
            return JsonResult.fail("操作失败:" + e.getMessage());
        }
    }
    @ApiOperation(value = "查询入库作业详情包装条码", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", value = "入库单id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getStoragePackLabel")
    public JsonResult<List<InventoryJobLabelVO>>  getStoragePackLabel(Long jobId){
        List<InventoryJobLabelVO> jobLabelVOList = inventoryInoutJobService.getStoragePackLabel(jobId);
        return JsonResult.success(jobLabelVOList);
    }

    @ApiOperation(value = "查询入库签收单", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "签收单编号", required = true, dataType = "String", paramType = "path")
            ,@ApiImplicitParam(name = "storageType", value = "入库类型", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/getDeliverySignInfo")
    public JsonResult<OrderDeliverySignVO>  getDeliverySignInfo(String code, String storageType){
        /*Integer signType = null;
        if("production".equals(storageType)){
            signType = 1;
        }else if("purchase".equals(storageType)){
            signType = 2;
        }*/
        OrderDeliverySignVO deliverySign = orderDeliverySignService.getDeliverySignInfo(code, 1);
        if(deliverySign == null){
            return JsonResult.fail("签收单不存在");
        }
        List<InventoryJobSignProductDetail> signProductList = inventoryJobSignProductDetailService.getSignProductBySignId(deliverySign.getId());
        deliverySign.setSignProductList(signProductList);
        return JsonResult.success(deliverySign);
    }

    @ApiOperation(value = "查询签收单产品信息和签收的条码总数", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signProductDetailId", value = "签收单产品id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getSignPackLabelNum")
    public JsonResult<InventoryJobLabelVO>  getSignPackLabelNum(Long signProductDetailId){
        InventoryJobLabelVO inventoryJobLabelVO = inventoryJobLabelService.getSignPackLabelNum(signProductDetailId);
        return JsonResult.success(inventoryJobLabelVO);
    }

}
